package org.example.order;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Service;

@Service
public class LibraryOrderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(LibraryOrderService.class);

    @Autowired
    private OrderStateMachineBuilder orderStateMachineBuilder;

    @Autowired
    private BeanFactory beanFactory;

    public void run(String... args) throws Exception {
        LOGGER.info("# 工作流开始");
        StateMachine<OrderStatus,OrderEvent> stateMachine = orderStateMachineBuilder.build(beanFactory);

        stateMachine.start();
        // 触发 支付，发货，收货 等事件
        stateMachine.sendEvent(OrderEvent.PAY);
        stateMachine.sendEvent(OrderEvent.DELIVER);
        stateMachine.sendEvent(OrderEvent.RECEIVE);
        stateMachine.stop();
    }
}
